<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:33:05
 * @LastEditTime: 2023-09-28 17:28:45
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Models;

use App\Repositories\FinanceVoucherRepository;
use App\Traits\MaterialTrait;
use App\Traits\AttachmentTrait;
use App\Traits\AutoCodeTrait;
use App\Traits\StorageTrait;
use App\Traits\VoucherTrait;

class MarketComplaint extends BaseModel
    {
    use AutoCodeTrait;
    use AttachmentTrait;
    use StorageTrait;
    use MaterialTrait;
    use VoucherTrait;
    public $timestamps = true;
    public $table = "sys_market_complaint";
    protected $guarded = [];
    //附件
    public $attachmentkey = "code";

    protected $casts = [];


    /**
     * 标识
     *
     * @var string
     */
    protected $ACSign = 'MARKET_COMPLAINT';

    /**
     * 编号所用的字段
     *
     * @var string
     */
    protected $ACCode = 'code';

    public function status()
        {
        return $this->belongsTo(Dic::class, "status_id", "key")->where(function ($q)
            {
            $q->where("code", 'status_with_finance');
            });
        }

    public function details()
        {
        return $this->hasMany(MarketComplaintDetails::class, "main_id");
        }
    public function red_retial()
        {
        return $this->belongsTo(MarketDeliveryDetailsPack::class, "red_id");
        }
    //发货明细单
    public function retails()
        {
        return $this->hasMany(MarketDeliveryDetails::class, "code", "code");
        }
    public function red()
        {
        return $this->belongsTo(MarketDeliveryDetailsPack::class);
        }
    public function complaint()
        {
        return $this->belongsTo(Dic::class, "complaint_id", "key")->where(function ($q)
            {
            $q->where("code", 'Complaint');
            });
        }
    public function department()
        {
        return $this->belongsTo(Department::class);
        }


    public function finance($orders = null)
        {
        $v = $orders ?? $this;
        if ($v->status_id == 2)
            return $v->voucher;
        if ($v->status_id != 1) {
            fail("单据非审核状态！，无法财审！");
            }
        //借应收账款
        //借应收账款
        //借应收账款
        //借应收账款
        //贷主营业务收入
        $credit_amount = 0;
        $details       = [];
        $v->retails->filter(function ($v) use (&$credit_amount, &$details)
            {
            $credit_amount += $v['se_amount'];
            $details[]     = FinanceVoucherRepository::mergeVoucher(1122, $v->finance_remark, $v['se_amount'], 0, array_merge($v->toArray(), [
                'company_id' => $v->customer_id,
                'price'      => $v->price
            ]));
            fauditSave($v);
            });

        $details[] = FinanceVoucherRepository::mergeVoucher(5001, $v['summary'], 0, $credit_amount);
        $voucher   = FinanceVoucherRepository::CreateVoucher($v->billdate ?? $v->created_at, 1, $details, $v, $v->voucher?->id);
        //审核凭证
        fauditSave($v);
        return FinanceVoucherRepository::auditVoucher($voucher);
        }

    public function nofinance($orders = null)
        {
        $v = $orders ?? $this;
        if ($v->status_id == 1)
            return $v->voucher;
        if ($v->status_id != 2) {
            fail("非财审状态！，无法取消财审！");
            }
        if (($v->voucher?->status_id ?? 0) != 0) {
            fail("凭证【{$v->voucher->code}】已记账！ 操作失败！");
            }
        $v->retails->filter(function ($v)
            {
            fauditSave($v, false);
            });

        return fauditSave($v, false);
        }
    }
